<?php

namespace App\Entities;

use App\Support\Database\Eloquent\UuidModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class Activity extends UuidModel
{
    use SoftDeletes;

    const CATEGORY_WECHAT = 1;
    const CATEGORY_H5 = 2;
    const LIMIT_DAY = 'day';
    const LIMIT_ALL = 'all';

    const PAUSED = 1;
    const RUNNING = 0;

    protected $fillable = [
        'id',
        'user_id',
        'category',
        'model_id',
        'template_id',
        'isPause',
        'clicks',
        'joins',
        'winnings',
        'name',
        'startTime',
        'endTime',
        'limit',
        'description',
        'ruleDescription',
        'listBackgroundImg',
        'shares',
        'rule',
        'group_code',
        'app_url',
        'publishing_ways',
        'remark',
        'organizer',
        'others',
        'customId',
    ];

    protected $dates = ['deleted_at'];

    public function model()
    {
        return $this->belongsTo(ActivityModel::class, 'model_id');
    }

    public function template()
    {
        return $this->belongsTo(ActivityModelTemplate::class, 'template_id');
    }

    public function prizes()
    {
        return $this->hasMany(ActivityPrize::class, 'activity_id');
    }

    public function groups()
    {
        return $this->hasMany(VoteGroup::class, 'activity_id');
    }

    /**
     * @return string|null
     */
    public function getId()
    {
        return $this->id;
    }

    public function translateColumns()
    {
        return [
            'id' => $this->getAttributeValue('id'),
            'user_id' => $this->getAttributeValue('user_id'),
            'category' => $this->getAttributeValue('category'),
            'model_id' => $this->getAttributeValue('model_id'),
            'template_id' => $this->getAttributeValue('template_id'),
            'is_pause' => $this->getAttributeValue('isPause'),
            'clicks' => $this->getAttributeValue('clicks'),
            'joins' => $this->getAttributeValue('joins'),
            'winnings' => $this->getAttributeValue('winnings'),
            'name' => $this->getAttributeValue('name'),
            'start_at' => $this->getAttributeValue('startTime'),
            'end_at' => $this->getAttributeValue('endTime'),
            'limits' => \GuzzleHttp\json_decode($this->getAttributeValue('limit')),
            'background_img' => $this->getAttributeValue('backgroundImg'),
            'background_color' => $this->getAttributeValue('backgroundColor'),
            'description' => $this->getAttributeValue('description'),
            'style_rule' => $this->getAttributeValue('style_rule'),
            'remark' => $this->getAttributeValue('remark'),
            'organizer' => $this->getAttributeValue('organizer'),
            'others' => $this->getAttributeValue('others'),
            'customId' => $this->getAttributeValue('customId'),
            'rule_description' => $this->getAttributeValue('ruleDescription'),
            'list_background_img' => $this->getAttributeValue('listBackgroundImg'),
            'shares' => \GuzzleHttp\json_decode($this->getAttributeValue('shares')),
            'rules' => \GuzzleHttp\json_decode($this->getAttributeValue('rules')),
            'group_code' => $this->getAttributeValue('group_code'),
            'created_at' => $this->getAttributeValue('created_at'),
            'updated_at' => $this->getAttributeValue('updated_at'),
        ];
    }
}